<template>
    <div>
      <!-- 显示当前评论的cell -->
      <van-cell>
        <template #title>
          <div class="cmt-cell">
            <div class="left-box">
              <van-image fit="cover" round :src="item.aut_photo" />

              <div class="info-box">
                <div class="name">{{ item.aut_name }}</div>
                <div class="content">{{ item.content }}</div>
                <div>
                  <span class="time">{{ $relvTime(item.pubdate) }}</span>
                </div>
              </div>
            </div>
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider>回复列表</van-divider>

      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell v-for="item in list" :key="item.com_id" :title="item.content">
          <template #title>
            <div class="cmt-cell">
              <div class="left-box">
                <van-image fit="cover" round :src="item.aut_photo" />

                <div class="info-box">
                  <div class="name">{{ item.aut_name }}</div>
                  <div class="content">{{ item.content }}</div>
                  <div>
                    <span class="time">{{ $relvTime(item.pubdate) }}</span>
                  </div>
                </div>
              </div>
            </div>
          </template>
        </van-cell>
      </van-list>

      <div class="field-box">
        <van-field
          @keyup.enter="send"
          v-model="cmt"
          center
          clearable
          placeholder="请输入评论内容"
        >
          <template #button>
            <van-button  @click="send" size="small" type="info">发布</van-button>
          </template>
        </van-field>
      </div>
    </div>
  </template>

<script>
import { commontAPI, sendCmtAPI } from '@/api/commont'
export default {
  props: {
    item: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      cmt: '',
      offset: null

    }
  },
  methods: {
    // 发送评论
    async send () {
      // 非空判断
      if (this.cmt === '') return this.$toast('请输入内容')
      // 不为空，要添加评论
      const res = await sendCmtAPI({
        // 发评论传文章id
        target: this.item.com_id,
        content: this.cmt,
        art_id: this.$route.params.id
      })
      // 把添加后的评论放到数组里即可
      this.list.unshift(res.data.new_obj)
      // 输入框清空
      this.cmt = ''
      this.$emit('add')
    },
    async onLoad () {
      const res = await commontAPI({
        // 因为现在获取的是评论，就要传a
        type: 'c',
        // 获取评论这里传文章id
        source: this.item.com_id,
        offset: this.offset,
        // 一次查10条
        limit: 10
      })

      // 把本次返回的最后一条存到offset里
      // 作为下一次查询的开始
      this.offset = res.data.last_id
      this.list.push(...res.data.results)
      // 把本次加载状态改为false
      this.loading = false

      // 判断是否全部加载完成
      if (this.list.length >= res.data.total_count) {
        this.finished = true
      }
    }
  }
}
</script>

  <style></style>
